﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_ucTaskIcon : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Task> allTasks = Task.GetAllTasks();
        Task selectedTask = GetSelectedTask(allTasks);
        int currentStatusTaskCount = Task.GetTaskCount(allTasks, selectedTask.EmpID, selectedTask.Status);
        EnableDisablePriorityButtons(selectedTask, currentStatusTaskCount);
    }

    protected void btnMove_Click(object sender, EventArgs e)
    {
        List<Task> allTasks = Task.GetAllTasks();
        Task selectedTask = GetSelectedTask(allTasks);
        bool moveSuccess;
        if (rdUser.Checked)
        {
            moveSuccess = selectedTask.MoveTaskToUser(Convert.ToInt32(ddlUser.SelectedValue));
        }
        else
        {
            Task.TaskStatus currStatus;
            if (Enum.TryParse(ddlStatus.SelectedValue, out currStatus))
            {
                moveSuccess = selectedTask.MoveTaskToStatus(currStatus);
            }
            else
            {
                moveSuccess = false;
            }
        }

        if (moveSuccess)
        {
            //TODO: move success msg
        }
        else
        {
            //TODO: move fail msg
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        List<Task> allTasks = Task.GetAllTasks();
        Task selectedTask = GetSelectedTask(allTasks);
        bool deleteSuccess = selectedTask.DeleteTask();
        if (deleteSuccess)
        {
            //TODO: Show success msg
        }
        else
        {
            //TODO: Show fail msg
        }
    }

    protected void btnDownPriority_Click(object sender, EventArgs e)
    {
        List<Task> allTasks = Task.GetAllTasks();
        Task selectedTask = GetSelectedTask(allTasks);
        int newPriority = selectedTask.DecreasePriority();
        lblPriorityDetails.Text = Convert.ToString(newPriority);
        modalPopupTaskDetails.Show();
    }

    protected void btnUpPriority_Click(object sender, EventArgs e)
    {
        List<Task> allTasks = Task.GetAllTasks();
        Task selectedTask = GetSelectedTask(allTasks);
        int newPriority = selectedTask.IncreasePriority();
        lblPriorityDetails.Text = Convert.ToString(newPriority);
        modalPopupTaskDetails.Show();
    }

    protected void radMove_CheckedChanged(object sender, EventArgs e)
    {
        List<Task> allTasks = Task.GetAllTasks();
        Task selectedTask = GetSelectedTask(allTasks);
        if (rdUser.Checked)
        {
            ddlStatus.SelectedIndex = ddlStatus.Items.IndexOf(ddlStatus.Items.FindByValue(Convert.ToString(selectedTask.Status)));
            ddlStatus.Enabled = false;
            ddlUser.Enabled = true;
        }
        else
        {
            ddlUser.SelectedIndex = ddlUser.Items.IndexOf(ddlUser.Items.FindByValue(Convert.ToString(selectedTask.EmpID)));
            ddlUser.Enabled = false;
            ddlStatus.Enabled = true;
        }
        modalPopupTaskDetails.Show();
    }

    private Task GetSelectedTask(List<Task> allTasks)
    {       
        Task selectedTask = allTasks
                            .Where(task => task.PBI == Convert.ToInt32(lblPBIDetails.Text) && task.EmpID == Convert.ToInt32(hdnEmpID.Value))
                            .FirstOrDefault();
        return selectedTask;
    }

    private void EnableDisablePriorityButtons(Task selectedTask, int currentStatusTaskCount)
    {
        if (currentStatusTaskCount == 1)
        {
            btnUpPriority.Enabled = false;
            btnDownPriority.Enabled = false;
        }
        else if (selectedTask.Priority == 1)
        {
            btnUpPriority.Enabled = false;
        }
        else if (selectedTask.Priority == currentStatusTaskCount)
        {
            btnDownPriority.Enabled = false;
        }
    }

}